package com.seo.service.sysRole.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.seo.mapper.sysRole.SysRoleMapper;
import com.seo.mapper.sysUser.SysUserMapper;
import com.seo.pojo.ReturnBody;
import com.seo.service.sysMenu.sdk.SysMenuService;
import com.seo.service.sysRole.sdk.SysRoleService;
import com.seo.utils.RightsHelper;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("sysRoleService")
public class SysRoleServiceImpl implements SysRoleService {

	@Autowired
	private SysRoleMapper sysRoleMapper;

	@Autowired
	private SysUserMapper sysUserMapper;
	
	@Autowired
	private SysMenuService sysMenuService;

	@Override
	public List<Map<String, Object>> getAllRoleList() {

		return sysRoleMapper.getAllRoleList();
	}

	@Override
	public int updateRoleRights(Map<String, Object> params) {
		// TODO Auto-generated method stub
		return sysRoleMapper.updateRoleRights(params);
	}

	@Override
	public Map<String, Object> getRoleById(Integer id) {
		// TODO Auto-generated method stub
		return sysRoleMapper.getRoleById(id);
	}

	@Override
	public ReturnBody getRoleMenu(Integer roleId, String type) {
		ReturnBody rb = ReturnBody.init();
		Map<String, Object> role = sysRoleMapper.getRoleById(roleId);
		if (role == null) {
			log.info("角色不存在");
			return rb.error("error", "角色不存在");
		}
		if (StringUtils.isEmpty(type) || !("add".equals(type) || "del".equals(type) || "edit".equals(type)
				|| "cha".equals(type) || "rights".equals(type))) {
			log.info("参数错误");
			return rb.error("error", "参数错误");
		}
		String rights = (String) role.get(type + "Qx");

		List<Map<String, Object>> menuList = sysMenuService.getAllMenuList();
		List<Map<String, Object>> nodeList = new ArrayList<>();

		for (Map<String, Object> menu : menuList) {
			Map<String, Object> node = new HashMap<>();
			boolean selected = RightsHelper.testRights(rights, (Integer) menu.get("id"));
			menu.put("selected", selected);
			node.put("id", menu.get("id"));
			Integer parentId = (Integer) menu.get("parentId");
			String parent = parentId == 0 ? "#" : String.valueOf(parentId);
			node.put("parent", parent);
			node.put("text", menu.get("name"));
			node.put("icon", menu.get("icon"));
			Map<String, Object> state = new HashMap<>();
			state.put("opened", true);
			state.put("selected", selected);
			node.put("state", state);
			nodeList.add(node);
		}
		rb.put("nodeList", nodeList);
		return rb.success();
	}

	@Override
	public ReturnBody getUserRoleMenu(String userType, Integer roleId) {
		ReturnBody rb = ReturnBody.init();
		String rights = "";
		if (!("00".equals(userType) || "01".equals(userType))) {
			log.info("参数错误");
			return rb.error("error", "参数错误");
		}

		if ("00".equals(userType)) {
			Map<String, Object> role = sysRoleMapper.getRoleById(roleId);
			if (role == null) {
				log.info("角色不存在");
				return rb.error("error", "角色不存在");
			}

			rights = (String) role.get("rightsQx");
			rb.put("role", role);
		}

		List<Map<String, Object>> menuList = sysMenuService.getAllMenuList();

		for (Map<String, Object> menu : menuList) {
			boolean selected = true;
			if ("00".equals(userType)) {
				selected = RightsHelper.testRights(rights, (Integer) menu.get("id"));
			}
			menu.put("selected", selected);
		}

		rb.put("menuList", menuList);
		return rb.success();
	}

	@Override
	public ReturnBody saveRole(Map<String, Object> params) {
		ReturnBody rb = ReturnBody.init();
		int num = sysRoleMapper.saveRole(params);
		if(num == 0){
			return rb.error("error","保存失败");
		}
		return rb.success();
	}

	@Override
	public ReturnBody updateRole(Map<String, Object> params) {
		ReturnBody rb = ReturnBody.init();
		int num = sysRoleMapper.updateRole(params);
		if(num == 0){
			return rb.error("error","保存失败");
		}
		return rb.success();
	}

	@Override
	public ReturnBody deleteRole(Integer roleId) {
		ReturnBody rb = ReturnBody.init();
		List<Map<String, Object>> userList = sysUserMapper.getSysUserByRoleId(roleId);
		if(userList != null && userList.size() > 0){
			return rb.error("该角色下已有用户");
		}
		
		int num = sysRoleMapper.deleteRole(roleId);
		
		if(num == 0){
			return rb.error("error","删除失败");
		}
		
		return rb.success();
	}

	
}
